<?php
    include_once('../src/classes/class.mysql.php');
    include_once('../src/classes/class.login.php');

    session_start();

    $login = new Login();

    if ($login->giveId() != $_GET['id'])
    {
     header('location: http://'.$_SERVER['HTTP_HOST'].'/');
    }
    
    $db = new MySQL();
    $result = $db->fetch_array($db->query("SELECT comtec_id_mainteiner FROM users WHERE comtec_id_user = ".$login->giveId()));   
    $comtec_id_mainteiner = $result['comtec_id_mainteiner'];
    
    $token = $_SESSION['token'] = md5(uniqid(mt_rand(), true));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta content="text/html; charset=utf-8" http-equiv="content-type">
 <title>RF Mantencion - Registros</title>
 <link rel="stylesheet" href="../js/general.css" type="text/css">    
 <link rel="stylesheet" href="../css/comtec.css" type="text/css">
 <script type="text/javascript" src="../js/jquery.js"></script>
 <script type="text/javascript">
 $(document).ready(function(){
   
   $("#userPanel").load("http://<?=$_SERVER['HTTP_HOST'];?>/div/userPanel.php?id=<?=$_GET['id'];?>");
   $("#enterpriseData").load("http://<?=$_SERVER['HTTP_HOST'];?>/div/enterpriseData.php?id=<?=$_GET['id'];?>");
    
   var mainMenu = '<ul class="mainMenu">';
   mainMenu += '<li><a href="http://<?=$_SERVER['HTTP_HOST'];?>/<?=$_SESSION['context']?>/panel.php?id=<?=$_GET['id'];?>">Inicio</a></li>';
   mainMenu += '<li><a href="http://<?=$_SERVER['HTTP_HOST'];?>/operations/index.php?id=<?=$_GET['id'];?>">Operaciones</a></li>';
   mainMenu += '<li><a href="http://<?=$_SERVER['HTTP_HOST'];?>/registers/index.php?id=<?=$_GET['id'];?>" style="color: yellow">Registros</a></li>';
   mainMenu += '<li><a href="http://<?=$_SERVER['HTTP_HOST'];?>/reports/index.php?id=<?=$_GET['id'];?>">Informes</a></li>';
   mainMenu += '</ul>';
   $("#mainMenu").html(mainMenu);
            
   $("#responsibles").click(function(){
     $("#subtitle").html('<h2>Registros - Encargados</h2>');
     var options = "<ul>\n";
     <? if($_SESSION["users"]!="clients" && $_SESSION["users"]!="technicians"){?>
     options += '<li><a href="javascript:loadAddResponsible()" id="addResponsible">Ingresar</a></li>\n';
     <? }?>
     options += '<li><a href="javascript:loadResponsibles()" id="listResponsibles">Listar</a></li>\n';
     options += "</ul>"
     $("#buttonPanel").html(options);
     loadResponsibles();
   });
  
   $("#machines").click(function(){
     $("#subtitle").html('<h2>Registros - Maquinas</h2>');
     var options = "<ul>\n";
     options += '<li><a href="javascript:loadAddMachine()" id="addMachine">Ingresar</a></li>\n';
     options += '<li><a href="javascript:loadMachines()" id="listMachines">Listar</a></li>\n';
     options += "</ul>"
     $("#buttonPanel").html(options);
     loadMachines();
   });
  
   $("#owners").click(function(){
     $("#subtitle").html('<h2>Registros - Propietarios</h2>');
     var options = "<ul>\n";
     options += '<li><a href="javascript:loadAddOwner()" id="addOwner">Ingresar</a></li>\n';
     options += '<li><a href="javascript:loadOwners()" id="listOwners">Listar</a></li>\n';
     options += "</ul>"
     $("#buttonPanel").html(options);
     loadOwners();
   });
  
   $("#confmans").click(function(){
     $("#subtitle").html('<h2>Registros - Configuraciones</h2>');
     var options = "<ul>\n";
     options += '<li><a href="javascript:loadAddConfigman()" id="addConfigman">Ingresar</a></li>\n';
     options += '<li><a href="javascript:loadConfigmans()" id="listConfigmans">Listar</a></li>\n';
     options += "</ul>"
     $("#buttonPanel").html(options);
     loadConfigmans();
   });

   $("#users").click(function(){
    $("#subtitle").html('<h2>Registros - Usuarios</h2>');
    var options = "<ul>\n";
    options += '<li><a href="javascript:loadAddUser()" id="addUser">Ingresar</a></li>\n';
    options += '<li><a href="javascript:loadUsers()" id="listUsers">Listar</a></li>\n';
    options += "</ul>"
    $("#buttonPanel").html(options);
    loadUsers();
   });
   
  });
 
 function loadAddResponsible(){
   $("#addResponsible").css("color","yellow");  
   $("#listResponsibles").css("color","white");   
   $.get("responsibles/loadAddResponsible.php",{token: $("#token").val()}, 
         function(data){
          $("#resultPanel").html(data);
        }
   );     
 }
 
 function loadAddOwner(){
   $("#addOwner").css("color","yellow");  
   $("#listOwners").css("color","white");    
   $.get("owners/loadAddOwner.php",{token: $("#token").val()}, 
         function(data){
          $("#resultPanel").html(data);
        }
   );     
 }
 
 function loadAddConfigman(){
  $("#addConfigman").css("color","yellow");  
  $("#listConfigmans").css("color","white");   
  $.get("configmans/loadAddConfigman.php",{token: $("#token").val()}, 
         function(data){
          $("#resultPanel").html(data);
        }
   );    
 }
 
 function loadAddUser(){
  $("#addUser").css("color","yellow");  
  $("#listUsers").css("color","white");  
  $.get("users/loadAddUser.php",{token: $("#token").val()}, 
         function(data){
          $("#resultPanel").html(data);
        }
   );    
  }
  
 function loadAddMachine(){
     $("#addMachine").css("color","yellow");  
     $("#listMachines").css("color","white");  
     $.get("machines/loadAddMachine.php",{token: $("#token").val(),id: $("#comtec_id_mainteiner").val()}, 
         function(data){
          $("#resultPanel").html(data);
        }
   );    
  }
 
 function responsible(comtec_id_responsible){
   $.get("responsibles/responsible.php", 
         {id: comtec_id_responsible},
         function(data){
          $("#resultPanel").html(data);
         }
   );        
 }
 
 function owner(comtec_id_owner){
   $.get("owners/owner.php", 
         {id: comtec_id_owner},
         function(data){
          $("#resultPanel").html(data);
         }
   );        
 }
 
 function configman(comtec_id_configman){
  $.get("configmans/configman.php", 
         {id: comtec_id_configman},
         function(data){
          $("#resultPanel").html(data);
         }
   );   
 }
 
 function user(comtec_id_user){
  $.get("users/user.php", 
         {id: comtec_id_user},
         function(data){
          $("#resultPanel").html(data);
         }
   );   
 }
 
  function machine(comtec_id_machine){
  $.get("machines/machine.php", 
         {id: comtec_id_machine},
         function(data){
          $("#resultPanel").html(data);
         }
   );   
 }
 
 function saveResponsible(){
  var position = $("#position").val();  
  var errores = '';
  var name = $("#name").val();
  if(name == ''){
    errores += '<p class="error">No ha ingresado: <b>Nombre</b><p>';        
  }
  var lastname1 = $("#lastname1").val();
  if(lastname1 == ''){
    errores += '<p class="error">No ha ingresado: <b>Apellido paterno</b><p>';        
  }
  var lastname2 = $("#lastname2").val();
  var cod_mainteiner = $("#cod_mainteiner").val();
  if( cod_mainteiner == ''){
    errores += '<p class="error">No ha ingresado: <b>C&oacute;digo</b><p>';     
  }
  var rut = $("#rut").val();
  if( rut == ''){
    errores += '<p class="error">No ha ingresado: <b>Rut</b><p>';     
  }
  var rut_cod = $("#rut_cod").val();
  if(rut_cod == '')
  {
   errores += '<p class="error">No ha ingresado: <b>Digito verificador</b><p>';    
  }
  
  var income_day = $("#income_day").val();
  if (income_day == '00'){
   errores += '<p class="error">No ha ingresado: <b>dia de la fecha de ingreso</b><p>';
  }
  var income_month = $("#income_month").val();
  if (income_month == '00'){
   errores += '<p class="error">No ha ingresado: <b>mes de la fecha de ingreso</b><p>';   
  }
  var income_year = $("#income_year").val();
  if (income_year == 'Año'){
   errores += '<p class="error">No ha ingresado: <b>a&ntilde;o de la fecha de ingreso</b><p>';  
  }
 
  var birthday_day = $("#birthday_day").val();
  if (birthday_day == '00'){
   errores += '<p class="error">No ha ingresado: <b>dia de la fecha de nacimiento</b><p>';
  }
  var birthday_month = $("#birthday_month").val();
  if (birthday_month == '00'){
   errores += '<p class="error">No ha ingresado: <b>mes de la fecha de nacimiento</b><p>';
  }
  var birthday_year = $("#birthday_year").val();
  if (birthday_year == 'Año'){
   errores += '<p class="error">No ha ingresado: <b>a&ntilde;o de la fecha de nacimiento</b><p>';
  }
  
  var email = $("#email").val();
  if (email == ''){
   errores += '<p class="error">No ha ingresado: <b>Correo electr&oacute;nico</b><p>';   
  }
  var password = $("#password").val();
  if (password == ''){
   errores += '<p class="error">No ha ingresado: <b>Contrase&ntilde;a</b><p>';   
  }
  
  var notes = $("#notes").val();
  var comtec_id_mainteiner = $("#comtec_id_mainteiner").val();
  var token = $("#token").val();
  var tag_id_responsible = $("#tag_id_responsible").val(); 
  if(errores == ''){
   $.post("../src/scripts/addResponsible.php", 
    {addResponsible: 1,comtec_id_mainteiner: comtec_id_mainteiner,name: name,
     lastname1: lastname1,lastname2: lastname2,
     token: token,position: position,notes: notes,cod_mainteiner: cod_mainteiner,
     rut: rut,rut_cod: rut_cod,birthday_day: birthday_day,birthday_month: birthday_month,birthday_year: birthday_year,
     income_day: income_day,income_month: income_month,income_year: income_year,tag_id_responsible: tag_id_responsible,
     email: email,password: password
    },
     function(data){
      $("#result").html(data);
     }
   ); 
  }else{
        $("#result").html(errores);
       }
 }
 
 function saveOwner(){
  var comtec_id_mainteiner = $("#comtec_id_mainteiner").val();
  var token = $("#token").val();
  var errores = '';
  var cod_owner = $("#cod_owner").val();
  if(cod_owner == ''){
    errores += '<p class="error">No ha ingresado: <b>C&oacute;digo</b><p>';        
  }
  var company_name = $("#company_name").val();
  if(company_name == ''){
    errores += '<p class="error">No ha ingresado: <b>Nombre</b><p>';        
  }
  var rut = $("#rut").val();
  if( rut == ''){
    errores += '<p class="error">No ha ingresado: <b>Rut</b><p>';     
  }
  var rut_cod = $("#rut_cod").val();
  if(rut_cod == '')
  {
   errores += '<p class="error">No ha ingresado: <b>Digito verificador</b><p>';    
  }
  
  var address = $("#address").val();
  var email = $("#email").val();
  var password = $("#password").val();
  var phone = $("#phone").val();
  var contact_name = $("#contact_name").val();
  var contact_lastname1 = $("#contact_lastname1").val();
  var contact_lastname2 = $("#contact_lastname2").val();
  var contact_position = $("#contact_position").val();
  if(errores == ''){
   $.post("../src/scripts/addOwner.php", 
    {addOwner: 1,contact_name: contact_name,contact_lastname1: contact_lastname1,contact_lastname2: contact_lastname2,contact_position: contact_position,address: address,comtec_id_mainteiner: comtec_id_mainteiner, token: token,cod_owner: cod_owner,
     company_name: company_name,rut: rut,rut_cod: rut_cod,email: email,password: password,phone: phone
    },
     function(data){
      $("#result").html(data);
     }
   );
  }else{
        $("#result").html(errores);
       }    
 }
 
 function saveConfigman(){  
  var comtec_id_mainteiner = $("#comtec_id_mainteiner").val();
  var token = $("#token").val();
  var errores = '';
  var configman_name = $("#configman_name").val();
  if(configman_name == ''){
   errores += '<p class="error">No ha ingresado: <b>Nombre configuraci&oacute;n</b><p>';        
  }
  var maintainment_period = $("#maintainment_period").val();
  if(maintainment_period == ''){
   errores += '<p class="error">No ha ingresado: <b>Per&iacute;odo de mantenci&oacute;n</b><p>';        
  }
  if (!((/^([0-9])*$/).test(maintainment_period)))
  {
   errores += '<p class="error">Ha ingresado un dato invalido en el <b>Per&iacute;odo de mantenci&oacute;n</b><p>';
  }
  var maintainment_day = $("#maintainment_day").val();
  if(maintainment_day == ''){
   errores += '<p class="error">No ha ingresado: <b>D&iacute;a de mantenci&oacute;n</b><p>';        
  }
  var maintainment_period_date = $("#maintainment_period_date").val();
  if(errores == ''){
  $.post("../src/scripts/addConfigman.php", 
    {addConfigman: 1,token: token,comtec_id_mainteiner: comtec_id_mainteiner,
     configman_name: configman_name, maintainment_period: maintainment_period,
     maintainment_day: maintainment_day, maintainment_period_date: maintainment_period_date
    },
     function(data){
      $("#result").html(data);
     }
   );
  }else{
        $("#result").html(errores);
       }
 }
 
 function saveUser(){
  var comtec_id_mainteiner = $("#comtec_id_mainteiner").val();
  var token = $("#token").val();
  var errores = '';
  var access_level = $("#access_level").val();
  if(access_level==0){
   errores += '<p class="error">No ha ingresado: <b>tipo de usuario</b><p>';     
  }
  var position = $("#position").val();
  var name = $("#name").val();
  var lastname1 = $("#lastname1").val();
  var lastname2 = $("#lastname2").val();
  var phone = $("#phone").val();
  var email = $("#email").val();
  if(email==''){
   errores += '<p class="error">No ha ingresado: <b>correo electr&oacute;nico</b><p>';     
  }
  var email1 = $("#email1").val();
  if(email1==''){
   errores += '<p class="error">No ha ingresado: <b>nuevamente el correo electr&oacute;nico</b><p>';     
  }
  var password = $("#password").val();
  if(password==''){
   errores += '<p class="error">No ha ingresado: <b>contrase&ntilde;a</b><p>';     
  }
  if(errores == ''){
   $.post("../src/scripts/addUser.php", 
    {addUser: 1,token: token,comtec_id_mainteiner: comtec_id_mainteiner,
     access_level: access_level,position: position,name: name,
     lastname1: lastname1,lastname2: lastname2,phone: phone,email: email,
     email1: email1,password: password
    },
     function(data){
      $("#result").html(data);
     }
   );
  }else{
         $("#result").html(errores);
        }
 }
 
 function saveMachine(){

  var comtec_id_mainteiner = $("#comtec_id_mainteiner").val();
  var token = $("#token").val();
  var errores = '';
  var comtec_id_owner = $("#comtec_id_owner").val();
   if (comtec_id_owner == 0)
   {
    errores += '<p class="error">No ha ingresado: <b>propietario</b><p>';       
   }
  var tag_id_machine = $("#tag_id_machine").val();
  var brand = $("#brand").val();
  var cod_mainteiner = $("#cod_mainteiner").val();
  if(cod_mainteiner == '' ){
   errores += '<p class="error">No ha ingresado: <b>Cod. mantenedor</b><p>';     
  }
  var model = $("#model").val();
  var year = $("#year").val();
  var comtec_id_responsible = $("#comtec_id_responsible").val();
   if (comtec_id_responsible == 0)
   {
    errores += '<p class="error">No ha ingresado: <b>Encargado</b><p>';       
   }
  var comtec_id_configman = $("#comtec_id_configman").val();
   if (comtec_id_configman == 0)
   {
    errores += '<p class="error">No ha ingresado: <b>Configuraci&oacute;n de mantenci&oacute;n</b><p>';       
   }
  var cod_owner = $("#cod_owner").val();
  var notes = $("#notes").val(); 
  var date_installation = $("#install_year").val()+$("#install_month").val()+$("#install_day").val();
  if(errores == ''){
  $.post("../src/scripts/addMachine.php", 
    {addMachine: 1,token: token,comtec_id_mainteiner: comtec_id_mainteiner,
     comtec_id_owner: comtec_id_owner,tag_id_machine: tag_id_machine,
     brand: brand,cod_mainteiner: cod_mainteiner,model: model,year: year,
     comtec_id_configman: comtec_id_configman,cod_owner: cod_owner,notes: notes,
     date_installation: date_installation,comtec_id_responsible: comtec_id_responsible
    },
     function(data){
      $("#result").html(data);
     }
  );
  }else{
         $("#result").html(errores);
        }
 }
 
 function loadResponsibles(){
   $("#addResponsible").css("color","white");  
   $("#listResponsibles").css("color","yellow");     
   $.get("responsibles/responsibles.php",{id: $("#comtec_id_mainteiner").val()},
         function(data){
          $("#resultPanel").html(data);
         }
   );     
 }
 
 function loadOwners(){
   $("#addOwner").css("color","white");  
   $("#listOwners").css("color","yellow");     
   $.get("owners/owners.php",{id: $("#comtec_id_mainteiner").val()},
         function(data){
          $("#resultPanel").html(data);
         }
   );     
 }
 
 function loadConfigmans(){
    $("#addConfigman").css("color","white");  
    $("#listConfigmans").css("color","yellow");    
    $.get("configmans/configmans.php",{id: $("#comtec_id_mainteiner").val()},
         function(data){
          $("#resultPanel").html(data);
         }
   );  
 }
 
 function loadMachines(){
    $("#addMachine").css("color","white");  
    $("#listMachines").css("color","yellow");   
    $.get("machines/machines.php",{id: $("#comtec_id_mainteiner").val()},
         function(data){
          $("#resultPanel").html(data);
         }
   );  
 }
 
 function loadUsers(){
  $("#addUser").css("color","white");  
  $("#listUsers").css("color","yellow");   
  $.get("users/users.php",{id: $("#comtec_id_mainteiner").val()},
         function(data){
          $("#resultPanel").html(data);
         }
   );  
 }
 
 function loadResponsible(comtec_id_responsible){
  var token = $("#token").val();
  $.get("responsibles/loadResponsible.php",{id: comtec_id_responsible,token: token},
  function(data){
   $("#resultPanel").html(data);
  }
  );   
 }
 
 function loadOwner(comtec_id_owner){
  var token = $("#token").val();
  $.get("owners/loadOwner.php",{id: comtec_id_owner,token: token},
  function(data){
   $("#resultPanel").html(data);
  }
  );   
 }
 
 function loadUser(comtec_id_user){
  var token = $("#token").val();
  $.get("users/loadUser.php",{id: comtec_id_user,token: token},
  function(data){
   $("#resultPanel").html(data);
  }
  );   
 }
 
 function loadMachine(comtec_id_machine){
  var token = $("#token").val();
  $.get("machines/loadMachine.php",{id: comtec_id_machine,token: token,mainteiner: $("#comtec_id_mainteiner").val()},
  function(data){
   $("#resultPanel").html(data);
  }
  );   
 }
 
 function loadConfigman(comtec_id_configman){
  var token = $("#token").val();
  $.get("configmans/loadConfigman.php",{id: comtec_id_configman,token: token},
  function(data){
   $("#resultPanel").html(data);
  }
  );   
 }
 
 function editResponsible(comtec_id_responsible){
  var errores = '';   
  var position = $("#position").val();  
  var name = $("#name").val();
  if(name == ''){
    errores += '<p class="error">No ha ingresado: <b>Nombre</b><p>';        
  }
  var lastname1 = $("#lastname1").val();
  if(lastname1 == ''){
    errores += '<p class="error">No ha ingresado: <b>Apellido paterno</b><p>';        
  }
  var lastname2 = $("#lastname2").val();
  var cod_mainteiner = $("#cod_mainteiner").val();
  if( cod_mainteiner == ''){
    errores += '<p class="error">No ha ingresado: <b>C&oacute;digo</b><p>';     
  }
  var rut = $("#rut").val();
  if( rut == ''){
    errores += '<p class="error">No ha ingresado: <b>Rut</b><p>';     
  }
  var rut_cod = $("#rut_cod").val();
  if(rut_cod == '')
  {
   errores += '<p class="error">No ha ingresado: <b>Digito verificador</b><p>';    
  }
  var income_day = $("#income_day").val();
  if (income_day == '00'){
   errores += '<p class="error">No ha ingresado: <b>dia de la fecha de ingreso</b><p>';
  }
  var income_month = $("#income_month").val();
  if (income_month == '00'){
   errores += '<p class="error">No ha ingresado: <b>mes de la fecha de ingreso</b><p>';   
  }
  var income_year = $("#income_year").val();
  if (income_year == 'Año'){
   errores += '<p class="error">No ha ingresado: <b>a&ntilde;o de la fecha de ingreso</b><p>';  
  }
  
  var birthdate_day = $("#birthdate_day").val();
  if ( birthdate_day == '00'){
   errores += '<p class="error">No ha ingresado: <b>dia de la fecha de nacimiento</b><p>';
  }
  var birthdate_month = $("#birthdate_month").val();
  if (birthdate_month == '00'){
   errores += '<p class="error">No ha ingresado: <b>mes de la fecha de nacimiento</b><p>';
  }
  var birthdate_year = $("#birthdate_year").val();
  if (birthdate_year  == '00'){
   errores += '<p class="error">No ha ingresado: <b>a&ntilde;o de la fecha de nacimiento</b><p>';
  }
  var notes = $("#notes").val();
  var comtec_id_mainteiner = $("#comtec_id_mainteiner").val();
  var token = $("#token").val();
  var tag_id_responsible = $("#tag_id_responsible").val();
  if(errores == ''){
  $.post("../src/scripts/editResponsible.php", 
    {editResponsible: 1,comtec_id_responsible: comtec_id_responsible,comtec_id_mainteiner: comtec_id_mainteiner,name: name,
     lastname1: lastname1,lastname2: lastname2,
     token: token,position: position,notes: notes,cod_mainteiner: cod_mainteiner,
     rut: rut,rut_cod: rut_cod,birthdate_day: birthdate_day,birthdate_month: birthdate_month,birthdate_year: birthdate_year,
     income_day: income_day,income_month: income_month,income_year: income_year,tag_id_responsible: tag_id_responsible
    },
     function(data){
      $("#result").html(data);
     }
  ); 
  }else{
        $("#result").html(errores);
       }    
 }
 
 function editOwner(comtec_id_owner){
  var token = $("#token").val();   
  var cod_owner = $("#cod_owner").val();
  var company_name = $("#company_name").val();
  var rut  = $("#rut").val();
  var rut_cod = $("#rut_cod").val();
  var address = $("#address").val();
  var contact_name = $("#contact_name").val();
  var contact_lastname1 = $("#contact_lastname1").val();
  var contact_lastname2 = $("#contact_lastname2").val();
  var contact_position = $("#contact_position").val();
  var phone = $("#phone").val();
  var email = $("#email").val();

  $.post("../src/scripts/editOwner.php", {editOwner: 1,cod_owner: cod_owner,company_name: company_name,rut: rut,
         rut_cod: rut_cod,address: address,contact_name: contact_name,contact_lastname1: contact_lastname1,
         contact_lastname2: contact_lastname2,contact_position: contact_position,phone: phone,email: email,
         comtec_id_owner: comtec_id_owner,token: token
         }, function(data){$("#result").html(data);}); 
 }
 
 function editUser(comtec_id_user){
  var token = $("#token").val();   
  var errores = '';
  var access_level = $("#access_level").val();
  if(access_level==0){
   errores += '<p class="error">No ha ingresado: <b>tipo de usuario</b><p>';     
  }
  var position = $("#position").val();
  var name = $("#name").val();
  var lastname1 = $("#lastname1").val();
  var lastname2 = $("#lastname2").val();
  var phone = $("#phone").val();
  var email = $("#email").val(); 
  if(email==''){
   errores += '<p class="error">No ha ingresado: <b>correo electr&oacute;nico</b><p>';     
  }
  if(errores == ''){
   $.post("../src/scripts/editUser.php", {editUser: 1, token: token, comtec_id_user: comtec_id_user,
         position: position,name: name,lastname1: lastname1,lastname2: lastname2,phone: phone,email: email,
         access_level: access_level
         }, function(data){$("#result").html(data);}); 
  }else{
         $("#result").html(errores);
        }        
 }
 
 function editMachine(comtec_id_machine){
  var token = $("#token").val();    
  var tag_id_machine = $("#tag_id_machine").val();   
  var brand = $("#brand").val();   
  var errores = '';
  var cod_mainteiner = $("#cod_mainteiner").val();
  if(cod_mainteiner == '' ){
   errores += '<p class="error">No ha ingresado: <b>Cod. mantenedor</b><p>';     
  }
  var model = $("#model").val();   
  var year = $("#year").val();   
  var comtec_id_owner = $("#comtec_id_owner").val();   
  if (comtec_id_owner == 0)
  {
   errores += '<p class="error">No ha ingresado: <b>propietario</b><p>';       
  }
  var comtec_id_configman = $("#comtec_id_configman").val(); 
  if (comtec_id_configman == 0)
   {
    errores += '<p class="error">No ha ingresado: <b>Configuraci&oacute;n de mantenci&oacute;n</b><p>';       
   }  
  var cod_owner = $("#cod_owner").val();   
  var notes = $("#notes").val();
  var comtec_id_responsible = $("#comtec_id_responsible").val(); 
  if (comtec_id_responsible == 0)
  {
   errores += '<p class="error">No ha ingresado: <b>Encargado</b><p>';       
  }
  var date_next_maintenance = $("#maintenance_year").val()+$("#maintenance_month").val()+$("#maintenance_day").val();
  if(errores == ''){
  $.post("../src/scripts/editMachine.php", {editMachine: 1, token: token, comtec_id_machine: comtec_id_machine,
         tag_id_machine: tag_id_machine,brand: brand,cod_mainteiner: cod_mainteiner,model: model,
         year: year,comtec_id_owner: comtec_id_owner,comtec_id_configman: comtec_id_configman,
         cod_owner: cod_owner,notes: notes,date_next_maintenance: date_next_maintenance,
         comtec_id_responsible: comtec_id_responsible
         }, function(data){$("#result").html(data);});
  }else{
         $("#result").html(errores);
        }        
 }
 
 function editConfigman(comtec_id_configman){
  var token = $("#token").val();   
  var errores = '';
  var configman_name = $("#configman_name").val();
  if(configman_name == ''){
   errores += '<p class="error">No ha ingresado: <b>Nombre configuraci&oacute;n</b><p>';        
  }
  var maintainment_period = $("#maintainment_period").val();
  if(maintainment_period == ''){
   errores += '<p class="error">No ha ingresado: <b>Per&iacute;odo de mantenci&oacute;n</b><p>';        
  }
  var maintainment_period_date = $("#maintainment_period_date").val();
  var maintainment_day = $("#maintainment_day").val();
  if(maintainment_day == ''){
   errores += '<p class="error">No ha ingresado: <b>D&iacute;a de mantenci&oacute;n</b><p>';        
  }
  if(errores == ''){
   $.post("../src/scripts/editConfigman.php", {editConfigman: 1, token: token, 
          comtec_id_configman: comtec_id_configman,configman_name: configman_name,
          maintainment_period: maintainment_period,maintainment_period_date: maintainment_period_date,
          maintainment_day: maintainment_day
         }, function(data){$("#result").html(data);}); 
  }else{
        $("#result").html(errores);
       }
 }
 
 function deleteResponsible(comtec_id_responsible){
  var token = $("#token").val();
  if(confirm('¿Esta seguro que desea eliminar el encargado?')){
    $.post("responsibles/deleteResponsible.php",{comtec_id_responsible: comtec_id_responsible,token: token},
      function(data){
       $("#result").html(data);
      }
    );
  }      
 }
 
 function deleteOwner(comtec_id_owner){
  var token = $("#token").val();
  if(confirm('¿Esta seguro que desea eliminar el propietario?')){
  $.post("owners/deleteOwner.php", {comtec_id_owner: comtec_id_owner,token: token}, 
    function(data){
     $("#result").html(data);
    }    
    );
  }      
 }
 
  function deleteConfigman(comtec_id_configman){
  var token = $("#token").val();
  if(confirm('¿Esta seguro que desea eliminar la configuracion?')){
  $.post("configmans/deleteConfigman.php", {comtec_id_configman: comtec_id_configman,token: token}, 
    function(data){
     $("#result").html(data);
    }    
    );
  }     
 }
 
 function deleteMachine(comtec_id_machine){
  var token = $("#token").val();
  if(confirm('¿Esta seguro que desea eliminar la maquina?')){
  $.post("machines/deleteMachine.php", {comtec_id_machine: comtec_id_machine,token: token}, 
    function(data){
     $("#result").html(data);
    }    
    );
  }      
 }
 
 function deleteUser(comtec_id_user){
  var token = $("#token").val();
  if(confirm('¿Esta seguro que desea eliminar el usuario?')){
  $.post("users/deleteUser.php", {comtec_id_user: comtec_id_user,token: token}, 
    function(data){
     $("#result").html(data);
    }    
    );
  }      
 }
 
 function ordenarMaquinas(op){
  $.get("machines/machines.php",{op: op, id: $("#comtec_id_mainteiner").val()},
         function(data){
          $("#resultPanel").html(data);
         }
        );     
 }
 
 </script>
</head>
<body>
    <div id="contenedor">
	<div id="title"><h1>RF Mantenci&oacute;n</h1><div id="enterpriseData"></div></div>
	<div id="userPanel"></div>
	<div id="mainMenu"></div>
	<div id="subtitle"><h2>Registros</h2></div>
	<div id="infoPanel"></div>
	<div id="buttonPanel">
	    <ul>
		<li><a href="#responsibles" id="responsibles">Encargados</a></li>
		<li><a href="#machines" id="machines">M&aacute;quinas</a></li>
		<li><a href="#owners" id="owners">Propietarios</a></li>
		<li><a href="#confmans" id="confmans">Configuraciones</a></li>
                <?if($_SESSION["users"]!="clients" && $_SESSION["users"]!="technicians"){?>
                 <li><a href="#users" id="users">Usuarios</a></li>
                <?}?>
	    </ul>
	</div>
        <input type="hidden" id="comtec_id_mainteiner" value="<?=$comtec_id_mainteiner;?>" />
        <input type="hidden" id="token" value="<?=$token;?>" />   
        <div id="resultPanel">        
<?  include_once '../div/footer.php';?>
